/** * Formats the supplied distance. * @function * @param {number} distanceToFormat The distance to format in crs units. * In this example the units are expected to be metres and will be formatted to distances under 1000m as metres rounded to the nearest metre. * Distances over 1000m will be formatted as kilometres and shown to 2 decimal places * @returns {string} The formatted distance. The formatted distance can include any valid html. */ function formatKilometres(distanceToFormat) { var formattedReturn = ''; if (distanceToFormat < 1000) { //round to the nearest integer distanceToFormat = Math.round(distanceToFormat); formattedReturn = distanceToFormat + 'm'; } else { //convert metres to km distanceToFormat /= 1000; //round to 2 decimal places distanceToFormat = Math.round(distanceToFormat * 100) / 100; formattedReturn = distanceToFormat + 'km'; } return formattedReturn; } /** * Formats the supplied distance. * @function * @param {number} distanceToFormat The distance to format in crs units. In this example the units are expected to be metres. * Distances under 100 yards will be in feet, over 100 yards but less than 1 mile will be in yards rounded to 2 decimal places. * Distances over 1 mile will be miles rounded to 2 decimal places * @returns {string} The formatted distance. The formatted distance can include any valid html. */ function formatMiles(distanceToFormat) { var formattedReturn = ''; //convert distanceToFormat from metres to feet var distanceInFeet = distanceToFormat * 3.28084; if (distanceInFeet < 300) {//less than 100 yards //round to the nearest integer distanceInFeet = Math.round(distanceInFeet); formattedReturn = distanceInFeet + 'ft'; } else if (distanceInFeet >= 300 && distanceInFeet < 5280) {//between 100yards and 1 mile //divide feet by 3 to get yards distanceInFeet /= 3; //round to 2 decimal places distanceInFeet = Math.round(distanceInFeet * 100) / 100; formattedReturn = distanceInFeet + 'yds'; } else {//over a mile //convert feet to miles distanceInFeet /= 5280; //round to 2 decimal places distanceInFeet = Math.round(distanceInFeet * 100) / 100; formattedReturn = distanceInFeet + 'miles'; } return formattedReturn; }